package com.zhao.dota.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zhao.dota.bo.AuthenticateUser;
import com.zhao.dota.model.DotaMatch;
import com.zhao.dota.model.User;
import com.zhao.dota.security.SecurityUtil;
import com.zhao.dota.service.UserService;
import com.zhao.dota.struct.Result;
import com.zhao.dota.struct.SimpleParameters;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
@RequestMapping("/user")
public class UserController {

    @Resource
    private UserService userService;

    @GetMapping("/current")
    public Result<AuthenticateUser> current() {
        AuthenticateUser authenticateUser = SecurityUtil.getAuthenticateUser();
        return Result.success(authenticateUser);
    }

    @GetMapping("/")
    public Result<Page<User>> page(Page<User> page) {
        Page<User> data = userService.page(page);
        return Result.success(data);
    }

    @GetMapping("/{id}")
    public Result<User> details(@PathVariable Integer id) {
        User user = userService.getById(id);
        return Result.success(user);
    }

    @PostMapping("/")
    public Result<User> save(User user) {
        if (user.getId() == null) {
            user.setPassword("123456");
            userService.signUp(user);
        } else {
            user.setPassword(null);
            user.setCreateTime(null);
            user.setSteamAuthorized(null);
            user.setEnabled(null);
            userService.updateById(user);
        }
        return Result.success(user);
    }

    @GetMapping("/resetPassword")
    public Result<Void> resetPassword(Integer id) {
        boolean b = userService.resetPassword(id);
        return Result.complete(b);
    }

}
